package leetcode.medium.maxalternatingsum;

import util.annotation.Solved;

/**
 * <a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/">
 *     1911. Maximum Alternating Subsequence Sum
 * </a>
 *
 * @author wangzefeng
 * @since 2023-07-11
 */
@Solved
public class Solution {
    public long maxAlternatingSum(int[] nums) {
        long even = nums[0], odd = 0;
        for (int i = 1; i < nums.length; i++) {
            even = Math.max(even, odd + nums[i]);
            odd = Math.max(odd, even - nums[i]);
        }
        return even;
    }
}
